import * as vscode from 'vscode';
import { I18nService } from '../services/i18nService';

export class I18nSemanticTokenProvider implements vscode.DocumentSemanticTokensProvider {
    private tokenTypes = new Map<string, number>([
        ['i18nKey', 0],
        ['invalidI18nKey', 1]
    ]);

    private tokenModifiers = new Map<string, number>();

    public readonly legend = new vscode.SemanticTokensLegend(
        ['i18nKey', 'invalidI18nKey'],
        []
    );

    constructor(private i18nService: I18nService) {}

    public provideDocumentSemanticTokens(
        document: vscode.TextDocument,
        _token: vscode.CancellationToken
    ): vscode.ProviderResult<vscode.SemanticTokens> {
        const builder = new vscode.SemanticTokensBuilder(this.legend);
        
        for (let i = 0; i < document.lineCount; i++) {
            const line = document.lineAt(i);
            const matches = Array.from(line.text.matchAll(/t\(['"]([\w\.]+)['"]\)/g));
            
            for (const match of matches) {
                const startPos = match.index! + 3; // Skip 't("'
                const translationKey = match[1];
                const length = translationKey.length;
                
                const tokenType = this.i18nService.isValidTranslationKey(translationKey)
                    ? 'i18nKey'
                    : 'invalidI18nKey';
                
                builder.push(
                    i,                              // line
                    startPos,                       // character
                    length,                         // length
                    this.tokenTypes.get(tokenType)!, // tokenType
                    0                               // tokenModifiers
                );
            }
        }
        
        return builder.build();
    }
} 